package com.jerry.utils.str;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Component
public class Jutils {
    
    private static final Logger log = LoggerFactory.getLogger(Jutils.class);
    
    private static Properties properties;
    
    public static String getProperty(String key){
        
        
        if (properties==null) {//双重锁模式  
            
            synchronized(Jutils.class){  
                
                if(properties==null){  
                    
                    properties = new Properties();
                    
                }  
            }  
            
            try {
               
            	
                FileInputStream stream = new FileInputStream("/config/application.properties");
                
                properties.load(stream);
                
                stream.close();
                
            } catch (FileNotFoundException e) {
                
                log.error(e.getMessage());
                
                e.printStackTrace();
                
            } catch (IOException e) {
                
                log.error(e.getMessage());
                
                e.printStackTrace();
                
            }
            
        } 
        if(!getEnv(key).equals("")) {
        	return getEnv(key);
        }else if(!SpringBootEnv.getKey(key).equals("")) {
        	return  SpringBootEnv.getKey(key);
        }else {
        	return  properties.getProperty(key);
        }
    }
    
    
    public static String getEnv(String key) {
    	String ret= "";
    	if(System.getenv().get(key)!=null) {
    		ret=System.getenv().get(key);
    	}
    	return ret;
    }
    
    public static boolean ipCheck(String text) {
        if (text != null && !text.isEmpty()) {
            // 定义正则表达式
            String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                      + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                      + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                      + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
            // 判断ip地址是否与正则表达式匹配
            if (text.matches(regex)) {
                // 返回判断信息
                return true;
            } else {
                // 返回判断信息
                return false;
            }
        }
        return false;
    }
    
	public static boolean isNumeric(String str){
		Pattern pattern = Pattern.compile("[0-9]*");

		Matcher isNum = pattern.matcher(str);

		return isNum.matches();

		}
    
    public static void main (String [] args) {
    	//System.out.println(Jutils.getConfig("zbx.user"));
    }
}
